﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BussinessLayer;

namespace projectQLDV
{
    public partial class frmHoatDong : DevExpress.XtraEditors.XtraForm
    {
        private frmMain frmM;
        private int idHoatdong;
        private string tenHD;
        private string thoigianHD;
        private int capHD;
        private string noidungHD;
        private string batbuocHD;
        private int hockiHD;
        private string namhocHD;
        tblhoatdong tblHD = new tblhoatdong();
        public frmHoatDong(frmMain frm)
        {
            InitializeComponent();
            frmM = frm;
            idHoatdong = -1;
            frmM.loadYear(this.comboBoxNamhocHD);
        }
        public frmHoatDong(frmMain frm,int _idhoatdong,string _tenhoatdong,string _thoigianHoatdong,int _caphoatdong,string _noidung,string _batbuoc,int _hocki,string _namhoc)
        {
            InitializeComponent();
            frmM = frm;
            idHoatdong = _idhoatdong;
            tenHD = _tenhoatdong;
            thoigianHD = _thoigianHoatdong;
            capHD = _caphoatdong;
            noidungHD = _noidung;
            batbuocHD = _batbuoc;
            hockiHD = _hocki;
            namhocHD = _namhoc;
            frmM.loadYear(this.comboBoxNamhocHD);
            showform();
        }

        private void frmHoatDong_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (idHoatdong == -1)
                frmM.frmThemHD_isShown = false;
            else
                frmM.frmCapNhatHD_isShown = false;
        }

        private void btnlammoi_Click(object sender, EventArgs e)
        {
            txtHoatdong.Text = "";
            txtNoidung.Text = "";
            datehoatdong.Text = "";
            checkbatbuoc.Checked = false;
            cbxCap.Text = "";
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            int _cap;
            bool _batbuoc = false;
            int _hocki=0;
            if (txtHoatdong.Text == "" || txtNoidung.Text == "" || datehoatdong.Text == "" || cbxCap.Text == "")
            {
                MessageBox.Show("Chưa nhập đầy đủ thông tin", "Chú Ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                try
                {
                    DataTable tbhoatdong = new DataTable();
                    tbhoatdong.Clear();

                    if (cbxCap.Text == "Đoàn trường")
                    {
                        _cap = 1;
                    }
                    else
                    {
                        _cap = 2;
                    }
                    if (radHocky1.Checked == true)
                        _hocki = 1;
                    if (radHocky2.Checked == true)
                        _hocki = 2;
                    _batbuoc = checkbatbuoc.Checked;
                    tblHD.hoatdong(this.idHoatdong, txtHoatdong.Text, DateTime.Parse(datehoatdong.Text).Date, _cap, txtNoidung.Text, _batbuoc, _hocki, comboBoxNamhocHD.Text, true);
                    MessageBox.Show("Lưu thành công!!!", "Thông báo");
                    frmM.GridViewHoatdong(comboBoxNamhocHD.Text);
                        //txtNoidung.Text = "";
                        //txtHoatdong.Text = "";
                        //datehoatdong.Text = "";
                        //cbxCap.Text = "";
                        //checkbatbuoc.Checked = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void btnLuuVaThoat_Click(object sender, EventArgs e)
        {
            btnLuu_Click(sender, e);
            btnThoat_Click(sender, e);

        }
        public void showform()
        {
            txtHoatdong.Text = tenHD;
            txtNoidung.Text = noidungHD;
            datehoatdong.Text = thoigianHD;
            if (capHD == 1)
                cbxCap.Text = "Đoàn trường";
            else
                cbxCap.Text = "Chi đoàn";
            if (batbuocHD == "True")
                checkbatbuoc.Checked = true;
            else
                checkbatbuoc.Checked = false;
            btnlammoi.Visible = false;
            if (hockiHD == 1)
                radHocky1.Checked = true;
            else
                radHocky2.Checked = true;
            comboBoxNamhocHD.Text = namhocHD;

        }
    }
}